package per.zhang.vaccinesys.serverConsumer.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import per.zhang.vaccinesys.serverConsumer.base.dto.ResultDTO;
import per.zhang.vaccinesys.serverConsumer.base.page.PageList;
import per.zhang.vaccinesys.serverConsumer.base.utils.AppointmentStateCode;
import per.zhang.vaccinesys.serverConsumer.base.utils.ResultUtil;
import per.zhang.vaccinesys.serverConsumer.dto.request.AppointmentRequestDTO;
import per.zhang.vaccinesys.serverConsumer.dto.respone.AppointmentRespondDTO;
import per.zhang.vaccinesys.serverConsumer.entity.Appointment;
import per.zhang.vaccinesys.serverConsumer.service.AppointmentService;

import java.util.Date;

/**
 * @auther zhang
 * @ TO DO
 * @creatTime 2021/1/26 20:38
 **/
@RestController
@RequestMapping("/appointment")
@CrossOrigin
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;


    /**
     * 用户预约疫苗
     * 用户查看预约
     * 用户取消预约
     */

    @PostMapping("/userAppointment")
    @ApiOperation(value = "用户预约打疫苗")
    //@ApiImplicitParams()
    public ResultDTO userAppointment(@RequestBody Appointment appointment)throws Exception{
        Appointment result = null;
        ResultDTO resultDTO = null;
        try {
            appointment.setCreatTime(new Date());
            appointment.setState(AppointmentStateCode.APPLICATIONING);

            /*result = appointmentService.userAppointment(appointment).getData();*/
            resultDTO = appointmentService.userAppointment(appointment);
            System.out.println(resultDTO.toString());
            if (resultDTO.getCode().equals("500")) {
                return resultDTO;
            }
        } catch (Exception e) {
            return ResultUtil.Error("500",e.getMessage());
        }
        return resultDTO;
    }


    @PostMapping("/getAppointmentPage")
    @ApiOperation("用户查看自己的接种历史")
    public ResultDTO selectEntityPage(@RequestBody AppointmentRequestDTO appointmentRequestDTO)throws Exception{
        PageList<AppointmentRespondDTO> pageList = null;
        ResultDTO<PageList<AppointmentRespondDTO>> resultDTO = null;
        try {
            resultDTO = appointmentService.selectEntityPage(appointmentRequestDTO);
            if (resultDTO.getCode().equals("500")) {
                return resultDTO;
            }
            pageList = resultDTO.getData();
            for (AppointmentRespondDTO item : pageList.getData()) {
                item.setState(AppointmentStateCode.STATE_CODE_MAP.get(item.getState()));
            }
        } catch (Exception e) {
            return ResultUtil.Error("500",e.getMessage());
        }
        return ResultUtil.Success(pageList);
    }

    @PostMapping("/userCancelAppointment")
    @ApiOperation(value = "用户取消打疫苗")
    //@ApiImplicitParams()
    public ResultDTO userCancelAppointment( @RequestBody Appointment appointment)throws Exception{
        /*Appointment result = null;*/
        ResultDTO<Appointment> resultDTO = null;
        try {
            /*result = appointmentService.userCancelAppointment(appointment).getData();*/
            resultDTO = appointmentService.userCancelAppointment(appointment);
            if (resultDTO.getCode().equals("500")) {
                return resultDTO;
            }
        } catch (Exception e) {
            return ResultUtil.Error("500",e.getMessage());
        }
        return resultDTO;
    }

}
